/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InterfaceUsuario.java
 *
 * Created on 08/07/2009, 15:06:08
 */
package interfaceGraficaUsuario;

import controle.Sistema;
import controle.UsuariosEnum;
import java.awt.Color;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author João Paulo
 */
public class AddUser extends javax.swing.JFrame {

    private UsuariosEnum tipo = UsuariosEnum.INEXISTENTE;

    /** Creates new form InterfaceUsuario */
    public AddUser() {
        initComponents();
    }

    /** Creates new form InterfaceUsuario com tipo de usuario*/
    public AddUser(UsuariosEnum tipo) {
        initComponents();
        this.tipo = tipo;
        setTitle("Adicionar Usuário");
        labelAddUser.setText("Preencha os campos abaixo para adicionar um  " + tipo.toString().toLowerCase());
        painelAddUsuario.setVisible(true);
        erro.setForeground(Color.RED);
        sucesso.setForeground(Color.BLUE);
    //erro.setText(ex.getMessage());
    //erro.setVisible(false);


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        painelAddUsuario = new javax.swing.JPanel();
        labelLogin = new javax.swing.JLabel();
        labelSenha = new javax.swing.JLabel();
        labelMatricula = new javax.swing.JLabel();
        labelNome = new javax.swing.JLabel();
        labelEmail = new javax.swing.JLabel();
        botaoSalvar = new javax.swing.JButton();
        botaoCancelar = new javax.swing.JButton();
        campoLogin = new javax.swing.JTextField();
        campoSenha = new javax.swing.JTextField();
        campoMatricula = new javax.swing.JTextField();
        campoNome = new javax.swing.JTextField();
        campoEmail = new javax.swing.JTextField();
        labelTurma = new javax.swing.JLabel();
        campoTurma = new javax.swing.JTextField();
        erro = new javax.swing.JLabel();
        labelAddUser = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        sucesso = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(550, 520));

        painelAddUsuario.setMinimumSize(new java.awt.Dimension(550, 520));
        painelAddUsuario.setPreferredSize(new java.awt.Dimension(550, 350));

        labelLogin.setText("Login:");

        labelSenha.setText("Senha:");

        labelMatricula.setText("Matricula:");

        labelNome.setText("Nome:");

        labelEmail.setText("E-Mail:");

        botaoSalvar.setText("Salvar");
        botaoSalvar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                botaoSalvarMouseClicked(evt);
            }
        });

        botaoCancelar.setText("Cancelar");
        botaoCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoCancelarActionPerformed(evt);
            }
        });

        labelTurma.setText("Turma:");

        labelAddUser.setText("Preencha os campos abaixo para adicionar um ...");

        jButton1.setText("Limpar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        sucesso.setFont(new java.awt.Font("Tahoma", 0, 12));

        javax.swing.GroupLayout painelAddUsuarioLayout = new javax.swing.GroupLayout(painelAddUsuario);
        painelAddUsuario.setLayout(painelAddUsuarioLayout);
        painelAddUsuarioLayout.setHorizontalGroup(
            painelAddUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelAddUsuarioLayout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addGroup(painelAddUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(painelAddUsuarioLayout.createSequentialGroup()
                        .addGroup(painelAddUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(painelAddUsuarioLayout.createSequentialGroup()
                                .addGroup(painelAddUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(painelAddUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(labelMatricula, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(labelTurma, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(labelEmail, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(labelNome, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(labelSenha, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addComponent(labelLogin))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(painelAddUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(painelAddUsuarioLayout.createSequentialGroup()
                                        .addComponent(botaoSalvar)
                                        .addGap(48, 48, 48)
                                        .addComponent(jButton1)
                                        .addGap(48, 48, 48)
                                        .addComponent(botaoCancelar)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE))
                                    .addComponent(campoLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
                                    .addComponent(campoSenha, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
                                    .addComponent(campoMatricula, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
                                    .addComponent(campoNome, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
                                    .addComponent(campoEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
                                    .addComponent(campoTurma, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE))
                                .addGap(91, 91, 91))
                            .addComponent(labelAddUser, javax.swing.GroupLayout.PREFERRED_SIZE, 462, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(293, 293, 293))
                    .addGroup(painelAddUsuarioLayout.createSequentialGroup()
                        .addGroup(painelAddUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(sucesso, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(erro, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        painelAddUsuarioLayout.setVerticalGroup(
            painelAddUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelAddUsuarioLayout.createSequentialGroup()
                .addGap(54, 54, 54)
                .addComponent(erro, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sucesso, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(52, 52, 52)
                .addComponent(labelAddUser)
                .addGap(18, 18, 18)
                .addGroup(painelAddUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campoLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelLogin))
                .addGap(18, 18, 18)
                .addGroup(painelAddUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelSenha)
                    .addComponent(campoSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(painelAddUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelMatricula)
                    .addComponent(campoMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(painelAddUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNome)
                    .addComponent(campoNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(painelAddUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelEmail)
                    .addComponent(campoEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(painelAddUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelTurma)
                    .addComponent(campoTurma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(painelAddUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaoSalvar)
                    .addComponent(botaoCancelar)
                    .addComponent(jButton1))
                .addContainerGap(82, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 570, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(painelAddUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 543, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(painelAddUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, 521, Short.MAX_VALUE)
                    .addContainerGap()))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void botaoSalvarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botaoSalvarMouseClicked

        if (criarUsuario(tipo)) {
            sucesso.setText(Sistema.addUserSucesso);
            erro.setText("");
            inicializaCamposUsuario();
        } else {
            erro.setText(Sistema.excecao);
            sucesso.setText("");
        }


    }//GEN-LAST:event_botaoSalvarMouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        inicializaCamposUsuario();
}//GEN-LAST:event_jButton1ActionPerformed

    private void botaoCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoCancelarActionPerformed
        dispose();
    }//GEN-LAST:event_botaoCancelarActionPerformed

    /**
     * cria um usuario (aluno ou moderador)
     * @param tipo - tipo de usuario
     * @throws IOException
     */
    public boolean criarUsuario(UsuariosEnum tipo) {
        List<String> dadosUsuario = new ArrayList<String>();

        dadosUsuario.add(campoLogin.getText());
        dadosUsuario.add(campoSenha.getText());
        dadosUsuario.add(campoMatricula.getText());
        dadosUsuario.add(campoNome.getText());
        dadosUsuario.add(campoEmail.getText());
        dadosUsuario.add(campoTurma.getText());

        try {
            return Sistema.addUsuario(dadosUsuario, tipo);
        } catch (Exception e) {
            erro.setText(e.getMessage());
        }
        return false;
    }

    /**
     * inicializa o valor dos campos de adicionar usuario, ou seja,
     * reseta o valor para vazio
     */
    public void inicializaCamposUsuario() {
        campoLogin.setText("");
        campoSenha.setText("");
        campoMatricula.setText("");
        campoNome.setText("");
        campoEmail.setText("");
        campoTurma.setText("");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AddUser().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    javax.swing.JButton botaoCancelar;
    javax.swing.JButton botaoSalvar;
    javax.swing.JTextField campoEmail;
    javax.swing.JTextField campoLogin;
    javax.swing.JTextField campoMatricula;
    javax.swing.JTextField campoNome;
    javax.swing.JTextField campoSenha;
    javax.swing.JTextField campoTurma;
    javax.swing.JLabel erro;
    private javax.swing.JButton jButton1;
    javax.swing.JLabel labelAddUser;
    javax.swing.JLabel labelEmail;
    javax.swing.JLabel labelLogin;
    javax.swing.JLabel labelMatricula;
    javax.swing.JLabel labelNome;
    javax.swing.JLabel labelSenha;
    javax.swing.JLabel labelTurma;
    javax.swing.JPanel painelAddUsuario;
    javax.swing.JLabel sucesso;
    // End of variables declaration//GEN-END:variables
}
